package xframe.util;

import java.io.File;
import java.util.Hashtable;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import xframe.init.GlobalVariable;

	/**
	* �ý��۸� :
	* ���α׷���: heap �޸� ������ �ǽð����� ����� �� �ִ� ������ �о�帮�� Ŭ����
	 * ���α׷� ���� :
	 * version : 1.0
	 * �ۼ��� : 2005. 7. 18.  ���� 6:7:50
	 * <p/>
	 * �ۼ���: ���鼮
	 * Ư�̻���:
	 * Version ������� :
	 */
	public class DynamicVar {
	    public static Hashtable GLOBAL_PROPERTIES_HT;
	    //public static String GLOBAL_PROPERTIES_ROOT ="D:\\my_project\\velocity\\WEB-INF\\xml_query\\";
	    public static String GLOBAL_PROPERTIES_ROOT =GlobalVariable.GLOBAL_VARIABLE_XML_ROOT;
	    static Logger log = Logger.getLogger(DynamicVar.class);


	    public static String get(String keyValue) throws XMLQueryException {
	        if(keyValue.matches(".")){
	            throw new XMLQueryException(keyValue.split(".").length+"[variables file name Error]"+keyValue+" has no '.' . \n Please check '"+keyValue+"' .");
	        }

	        PropertiesValue proValue =null;
	        String propertiesFileName =keyValue.substring(0, keyValue.indexOf("."));
	        String propertiesIndexKey = keyValue.substring(keyValue.indexOf(".")+1, keyValue.length());
	        File propertiesFile = new File(GLOBAL_PROPERTIES_ROOT+propertiesFileName+".dynamic_var");
	        long lastmodified = propertiesFile.lastModified();


	        if(GLOBAL_PROPERTIES_HT ==null){
	            GLOBAL_PROPERTIES_HT = new Hashtable();
	        }
	        //keyValue : person.PePEBUSI01_DELETE_2
	        //propertiesFileName:person
	        //propertiesIndexKey:PePEBUSI01_DELETE_2

	        if(GLOBAL_PROPERTIES_HT.containsKey(propertiesFileName)){
	            proValue = (PropertiesValue)GLOBAL_PROPERTIES_HT.get(propertiesFileName);
	            //System.out.println("������ ������ ��");
	            //������ ������ ��
	            if(lastmodified ==proValue.getLastmodifiedDate() ){
	                //System.out.println("�������� ������ �޸𸮿� ������ ���� �״�� ����");
	                //�������� ������ �޸𸮿� ������ ���� �״�� ����.
	            }else{
	                // System.out.println("lastmodified ==xmlQuery.getLastmodifiedDate()\n"+lastmodified +" :"+xmlQuery.getLastmodifiedDate());
	                //System.out.println("�������� �ٸ��� xml �� �ٽ� �о �޸𸮿� �����.");
	                //�������� �ٸ��� xml �� �ٽ� �о �޸𸮿� �����.
	                GLOBAL_PROPERTIES_HT.remove(propertiesFileName);
	                PropertiesValue newProperties = new PropertiesValue(GLOBAL_PROPERTIES_ROOT+propertiesFileName+".dynamic_var");
	                GLOBAL_PROPERTIES_HT.put(propertiesFileName, newProperties);
	            }
	        }else{
	            // System.out.println("["+propertiesFileName+"]���� �޸𸮿��� �ش�Ű���� xml�� �ٽ� �д´�.");
	            // System.out.println("GLOBAL_PROPERTIES_HT\n"+GLOBAL_PROPERTIES_HT);
	            PropertiesValue newProperties = new PropertiesValue(GLOBAL_PROPERTIES_ROOT+propertiesFileName+".dynamic_var");
	            GLOBAL_PROPERTIES_HT.put(propertiesFileName, newProperties);
	        }


	        //������ GLOBAL_PROPERTIES_HT ���� �ٽ� �˻��Ѵ�..
	        PropertiesValue searchProperties = (PropertiesValue)GLOBAL_PROPERTIES_HT.get(propertiesFileName);

	        String value="";
	        // System.out.println("ã��Ű==>"+propertiesIndexKey);
	        // System.out.println("searchQuery==>"+searchQuery);
	        value=searchProperties.getProperties(propertiesIndexKey);


	        if(value ==null){
	            throw new XMLQueryException("[Variables Index Error]"+propertiesFileName+".dynamic_var has no '"+keyValue+"' \n Please check '"+propertiesFileName+".dynamic_var' .");
	        }
	        return value;
	    }

	    public static String get(String keyValue, String defaultValue) throws XMLQueryException {

	        PropertiesValue proValue =null;
	        String propertiesFileName =keyValue.substring(0, keyValue.indexOf("."));
	        String propertiesIndexKey = keyValue.substring(keyValue.indexOf(".")+1, keyValue.length());
	        File propertiesFile = new File(GLOBAL_PROPERTIES_ROOT+propertiesFileName+".dynamic_var");
	        long lastmodified = propertiesFile.lastModified();


	        if(GLOBAL_PROPERTIES_HT ==null){
	            GLOBAL_PROPERTIES_HT = new Hashtable();
	        }

	        if(GLOBAL_PROPERTIES_HT.containsKey(propertiesFileName)){
	            proValue = (PropertiesValue)GLOBAL_PROPERTIES_HT.get(propertiesFileName);
	            //System.out.println("������ ������ ��");
	            //������ ������ ��
	            if(lastmodified ==proValue.getLastmodifiedDate() ){
	                //System.out.println("�������� ������ �޸𸮿� ������ ���� �״�� ����");
	                //�������� ������ �޸𸮿� ������ ���� �״�� ����.
	            }else{
	                // System.out.println("lastmodified ==xmlQuery.getLastmodifiedDate()\n"+lastmodified +" :"+xmlQuery.getLastmodifiedDate());
	                //System.out.println("�������� �ٸ��� xml �� �ٽ� �о �޸𸮿� �����.");
	                //�������� �ٸ��� xml �� �ٽ� �о �޸𸮿� �����.
	                GLOBAL_PROPERTIES_HT.remove(propertiesFileName);
	                PropertiesValue newProperties = new PropertiesValue(GLOBAL_PROPERTIES_ROOT+propertiesFileName+".dynamic_var");
	                GLOBAL_PROPERTIES_HT.put(propertiesFileName, newProperties);
	            }
	        }else{
	            // System.out.println("["+propertiesFileName+"]���� �޸𸮿��� �ش�Ű���� xml�� �ٽ� �д´�.");
	            // System.out.println("GLOBAL_PROPERTIES_HT\n"+GLOBAL_PROPERTIES_HT);
	            PropertiesValue newProperties = new PropertiesValue(GLOBAL_PROPERTIES_ROOT+propertiesFileName+".dynamic_var");
	            GLOBAL_PROPERTIES_HT.put(propertiesFileName, newProperties);
	        }


	        //������ GLOBAL_PROPERTIES_HT ���� �ٽ� �˻��Ѵ�..
	        PropertiesValue searchProperties = (PropertiesValue)GLOBAL_PROPERTIES_HT.get(propertiesFileName);

	        String value="";
	        // System.out.println("ã��Ű==>"+propertiesIndexKey);
	        // System.out.println("searchQuery==>"+searchQuery);
	        value=searchProperties.getProperties(propertiesIndexKey);

	        if(value ==null){
	            System.out.println("[Variables Index Not Found]"+propertiesFileName+".dynamic_var has no '"+keyValue+"' .\n Use the default value["+defaultValue+"] .");
	            return defaultValue;
	        }
	        return value;
	    }


	    public static String getProperties(String prefix, String suffix) throws XMLQueryException {
	        PropertiesValue proValue =null;
	        String keyValue = prefix+"."+ suffix;
	        if(GLOBAL_PROPERTIES_HT !=null){
	            proValue = (PropertiesValue)GLOBAL_PROPERTIES_HT.get(prefix);
	        }else{
	            GLOBAL_PROPERTIES_HT = new Hashtable();
	        }

	        if(proValue == null){
	            proValue = new PropertiesValue(GLOBAL_PROPERTIES_ROOT+prefix+".dynamic_var");

	            GLOBAL_PROPERTIES_HT.put(keyValue, proValue);
	        }
	        PropertiesValue searchProperties = (PropertiesValue)GLOBAL_PROPERTIES_HT.get(keyValue);
	        return  searchProperties.getProperties(suffix);
	    }


	    public static String[] getXMLIndex(){
	        Set set = GLOBAL_PROPERTIES_HT.keySet();
	        Object obj[] = set.toArray();
	        String returnStr[] =  new String[obj.length];
	        for(int i =0 ; i < obj.length ;i++){
	            returnStr[i]=(String)obj[i];
	            //System.out.println(i+"="+returnStr[i]);
	        }
	        return returnStr;
	    }

	    public static String[] getArrayString(String index){
	        try {
	            String text = get(index);
	            return StringUtils.split(text, '|');
	        } catch (XMLQueryException e) {
	            e.printStackTrace();
	        }
	        return null;
	    }

	    public static String okGetString(String xml_index, String userText, String repalce_xml_index){
	        String permitStr[] = getArrayString(xml_index);
	         //for(int i =0 ; i < permitStr.length ;i++){
	          //  System.out.println(":> "+i+" : "+permitStr[i]);
	        //}
	        String temp="";
	        if(!UtilMaster.isThere(permitStr, userText)){
	            try {
	                temp=  get(repalce_xml_index);
	            } catch (XMLQueryException e) {
	                e.printStackTrace();
	            }
	        }
	        return temp;
	    }


	    public static String notGetString(String xml_index, String userText, String repalce_xml_index){
	        String permitStr[] = getArrayString(xml_index);
	        String temp="";
	        if(UtilMaster.isThere(permitStr, userText)){
	            try {
	                temp=  get(repalce_xml_index);
	            } catch (XMLQueryException e) {
	                e.printStackTrace();
	            }
	        }
	        return temp;
	    }

	    public static String trueAndFalse (String xml_index, String condition, String tureValue, String falseValue){
	        String permitStr[] = getArrayString(xml_index);
	        if(!UtilMaster.isThere(permitStr, condition)){
	            return tureValue;
	        }else{
	            return  falseValue;
	        }
	    }

	    public static void getStringOnCollection(String xml_index){
	        String temp[] = getArrayString(xml_index);
	        for(int i =0 ; i < temp.length ;i++){
	            System.out.println(":> "+i+" : "+temp[i]);
	        }
	    }

	}
